<?php 	
	namespace app\index\controller;

	use think\Controller;
	use think\Request;
	use think\Session;
	use think\Db;
	class Personal extends Base
	{	
		// private $obj;

		// public function _initialize()
		// {
		// 	$this->obj = model('zuser');
		// }
		
		public function index()
		{
	        $id = Session::get('index.id');
	        $data = db('zuser')->where('id',$id)->find();
			// $res = model('zuser')->find($id);
			return view('index@personal/personal',[
					'list' => $data
			]);
		}

		public function savee(Request $request){
			$data = input('post.');
			$file = $request->file('icon');
			$path = ROOT_PATH . 'public' . DS . 'uploads'. DS . 'icon';

			if($file){
		    $info = $file->validate(['ext'=>'jpg,png,gif'])->move($path);
		    if($info){
		        $info->getExtension();
		        // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
		        $info->getSaveName();
		        // 输出 42a79759f284b767dfcb2a0197904287.jpg
		        $info->getFilename(); 
		    }else{
		        // 上传失败获取错误信息
		        $file->getError();
		    }
			    $info = $info->getSaveName();
			    $info = str_replace("\\","/",$info);
			    $data['icon'] = $info;
			}


		// 调用tp5数据验证
			$validate = validate('Category');

			if(!$validate->scene('add')->check($data)){
				$this->error($validate->getError());
			}
			$res = db('zuser')->where('tel',$data['tel'])->update($data);
			if($res){
				$this->success('修改成功!');
			}else{
				$this->error('修改失败!');
			}


		}
		// 加载修改密码页面
		public function pwdhtml(){

	        $id = Session::get('index.id');
			return view('index@personal/changepwd',[
					'id' => $id
			]);
		}
		
		//执行修改密码
		public function savepwd($id){

			$id = $id;
			$info = input('post.');
			$pwd = intval($info['pwd']);
			$rpwd = $info['repwd'];
			$length = strlen($pwd);
			if($pwd == 0){
				$this->error('密码不能为空');
			}
			if($pwd == $rpwd){
				db('zuser')->where('id',$id)->update(['pwd'=>md5($pwd)]);
				$this->success('恭喜您,密码修改成功');
			}
		}


		// 生成订单
		public function createorder(){
			$info = input('post.');
			$info['status'] = 2; 
			$info['orderNum'] = rand().time();
			$info['create_time'] = time();
			$result = db('order')->insert($info);
			if($result == 0){
				$this->error('预约失败!');
			}else{
				$this->success('预约成功,请等待审核!','index/index/index');
			}
		}

		// 加载订单页面
		public function order(){

			$list = Db::name('zuser as z,lamp_order as o,lamp_yshop as y')
               ->field('o.id,o.orderNum,o.tel,o.text,y.name as yname,y.tel as ytel,y.address')
               ->where('o.uid = z.id and o.status = 2 and y.id = o.sid')
               ->select();
			return view('index@personal/order',[
				'list' => $list
			]);
		}

		public function readyorder(){
			$list = Db::name('zuser as z,lamp_order as o,lamp_yshop as y')
               ->field('o.id,o.orderNum,o.tel,o.text,y.name as yname,y.tel as ytel,y.address,z.id as zid,o.display')
               ->where('o.uid = z.id and o.status = 1 and y.id = o.sid')
               ->select();
			return view('index@personal/readyorder',[
				'list' => $list
			]);

		}

		// 取消预约
		public function delOrder($id){
			$data = db('order')->where('id',$id)->update(['status'=>'5']);
			if($data>0){
				$this->success('取消成功');
			}
		}
		// 显示评论页面
		public function commit($id,$pp){
			$zid = $id;
			$orid = $pp;
			$v['zid'] = $zid;
			$v['orid'] = $pp;
			return view('index@personal/commit',[
				'v' => $v
			]);
		}

		// 评论图片上传
		public function uploadPic($id,$pp){
			
			$info = input('post.');
			$info['zid'] = $id;
			$info['orid'] = $pp;
			$result = db('comment')->insert($info);
			if($result > 0 ){
				db('order')->where('id',$pp)->update(['display'=>1]);
				$this->success('评论成功','index/Personal/readyorder');
			}else{
				$this->error('评论失败,请重新评论');
			}
			return view('index@personal/commit');
		}
		// 加载个人评论模板
		public function perComment(){
	        $id = Session::get('index.id');
			$list = db('comment as c,lamp_yshop as y,lamp_order as o')
				->field('c.text,y.name,c.id')
				->where('c.status = 1 and y.id = o.sid and c.orid = o.id and uid='.$id)
				->select();
			return view('index@Personal/perComment',[
				'list' => $list
			]);
		}
		// 自己删除评论
		public function delComment($id){
			$data = db('comment')->where('id',$id)->update(['status'=>0]);
			if($data>0){
				$this->success('删除成功');
			}
		}

	}
	